package com.zpark.it.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpark.it.pojo.Student;
import com.zpark.it.pojo.Teacher;
import com.zpark.it.service.TeacherService;
import com.zpark.it.utils.MD5;
import com.zpark.it.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: TeacherController
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/31 16:56
 */
@Api(tags = "教师管理控制器")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {


    @Autowired
    private TeacherService teacherService;


    @ApiOperation("分页获取教师信息,带搜索条件")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(
            @ApiParam("页码数") @PathVariable("pageNo") Integer pageNo ,
            @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize ,
            @ApiParam("查询条件") Teacher teacher
    ){
        Page<Teacher> paraParam =new Page<>(pageNo,pageSize);

        IPage<Teacher> page = teacherService.getTeachersByOpr(paraParam,teacher);

        return Result.ok(page);
    }

    @ApiOperation("添加或者修改教师信息")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@ApiParam("修改教师的对象的json格式")@RequestBody Teacher teacher){
        //如果是新增要对密码进行加密
        Integer id = teacher.getId();
        if (id==null||id==0) {
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    @ApiOperation("删除教师信息")
    @DeleteMapping("/saveOrUpdateTeacher")
    public Result deleteTeacher(@ApiParam("删除的id的json格式")@RequestBody List<Integer> ids){
        teacherService.removeByIds(ids);
        return Result.ok();
    }
}
